package com.sbm.datasource.config;

import java.util.HashMap;
import java.util.Map;

import lombok.Data;

@Data
public class DataSourceConfig {
    private static final String DATASOURCE_TYPE_DEFAULT = "org.apache.tomcat.jdbc.pool.DataSource";

    @SuppressWarnings("unchecked")
    public DataSourceConfig(Map<String, Object> datasource) {
        if (datasource.containsKey("type"))
            this.setType(datasource.get("type").toString());
        else
            this.setType(DATASOURCE_TYPE_DEFAULT);
        this.setName(datasource.get("name").toString());
        this.setDriverClassName(datasource.get("driverClassName").toString());
        this.setUrl(datasource.get("url").toString());
        this.setUsername(datasource.get("username").toString());
        this.setPassword(datasource.get("password").toString());
        // 所有配置
        this.setAllConfig(datasource);
        // 附加配置
        Map<String, Object> map = (Map<String, Object>) ((HashMap<String, Object>) datasource).clone();
        map.remove("name");
        map.remove("type");
        map.remove("driverClassName");
        map.remove("url");
        map.remove("username");
        map.remove("password");
        this.setExtConfig(map);
    }

    private String name;
    private String type;
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private Map<String, Object> allConfig;
    private Map<String, Object> extConfig;
}
